﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Analyze.Service;
using Common.Dao;
using Common.Extension;
using log4net;
using Common.Model;
using Common.Util;
using static Newtonsoft.Json.JsonConvert;

namespace Analyze.Script
{
    public class DxwSegmScript
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(BlockFilterScript));
        
        public static void Run()
        {
            Log.Info("segm script start");
            List<DxwEvent> dxwEvents = DxwEventDao.GetEventsWithoutWords();

            var notKeywords = DxwEventDao.GetEventsWithoutKeywords();
            if (null != notKeywords)
            {
                foreach (var newse in notKeywords)
                {
                    var dictionary = KeywordService.GetKeywords(newse.Title + newse.Content);
                    newse.Keywords = SerializeObject(dictionary);
                }    
            }
            
            
            DxwEventDao.UpdateEventKeywords(notKeywords);
            
            if (null == dxwEvents || dxwEvents.Count==0)
            {
                Log.Info("segm script end");
                return;
            }
            List<DxwEvent> errorEvents = new List<DxwEvent>();
            List<List<DxwEvent>> eventsList = dxwEvents.Partition(50).ToList();
            foreach (var events in eventsList)
            {
                List<List<string>> words = null;
                try
                {
                    words = SegmentationService.GetWords(events.Select(m => m.Title + m.Content).ToArray());
                }
                catch (Exception e)
                {
                    errorEvents.AddRange(events);
                    Log.Error("segm error:");
                    Log.Error("events =" + errorEvents.Select(m => m.EventId).ToList().List2String());
                    continue;
                }
                
                for (var i = 0; i < events.Count; i++)
                {
                    var stringBuilder = new StringBuilder();
                    events[i].Words = Util.MergeWords(words[i]);
                }
                
                DxwEventDao.BatchSaveEventWords(events);
            }

            if (errorEvents.Count > 0)
            {
                Log.Info("retry======================================================retry");
                Log.Info("errorEvents count = " + errorEvents.Count);
                foreach (DxwEvent errorEvent in errorEvents)
                {
                    var dictionary = SegmentationService.GetWords(errorEvent.Content);

                    if (dictionary.Count == 0)
                    {
                        Log.Error("error:");
                        Log.Error("event_id=" + errorEvent.EventId);
                        Log.Error("content=" + errorEvent.Content);
                    }
                    else
                    {
                        errorEvent.Words = Util.MergeWords(dictionary);
                    }
                }
                DxwEventDao.BatchSaveEventWords(errorEvents);
            }
            Log.Info("segm script end");
        }
    }
}